var ResumeActions = {
   actions: {},
   defaults: {},
   icons: {},
   currentResume: null,
   register: function (mime, name, permissions, icon, action) {
      if (!ResumeActions.actions[mime]) {
         ResumeActions.actions[mime] = {};
      }
      if (!ResumeActions.actions[mime][name]) {
         ResumeActions.actions[mime][name] = {};
      }
      ResumeActions.actions[mime][name]['action'] = action;
      ResumeActions.actions[mime][name]['permissions'] = permissions;
      ResumeActions.icons[name] = icon;
   },
   setDefault: function (mime, name) {
      ResumeActions.defaults[mime] = name;
   },
   get: function (mime, type, permissions) {
      var actions = {};
      if (ResumeActions.actions.all) {
         actions = $.extend(actions, ResumeActions.actions.all);
      }
      if (mime) {
         if (ResumeActions.actions[mime]) {
            actions = $.extend(actions, ResumeActions.actions[mime]);
         }
         var mimePart = mime.substr(0, mime.indexOf('/'));
         if (ResumeActions.actions[mimePart]) {
            actions = $.extend(actions, ResumeActions.actions[mimePart]);
         }
      }
      if (type) {//type is 'dir' or 'file'
         if (ResumeActions.actions[type]) {
            actions = $.extend(actions, ResumeActions.actions[type]);
         }
      }
      var filteredActions = {};
      $.each(actions, function (name, action) {
         if (action.permissions & permissions) {
            filteredActions[name] = action.action;
         }
      });
      return filteredActions;
   },
   getDefault: function (mime, type, permissions) {
      if (mime) {
         var mimePart = mime.substr(0, mime.indexOf('/'));
      }
      var name = false;
      if (mime && ResumeActions.defaults[mime]) {
         name = ResumeActions.defaults[mime];
      } else if (mime && ResumeActions.defaults[mimePart]) {
         name = ResumeActions.defaults[mimePart];
      } else if (type && ResumeActions.defaults[type]) {
         name = ResumeActions.defaults[type];
      } else {
         name = ResumeActions.defaults.all;
      }
      var actions = this.get(mime, type, permissions);
      return actions[name];
   },
   getCurrentId: function() {
      return  ResumeActions.currentResume.parent().attr('data-id'); 
   }, 

   getCurrentResumeName: function () {
      return ResumeActions.currentResume.parent().attr('data-name');
   },
   getCurrentMimeType: function () {
      return ResumeActions.currentResume.parent().attr('data-mime');
   },
   getCurrentType: function () {
      return ResumeActions.currentResume.parent().attr('data-type');
   },
   getCurrentPermissions: function () {
      return ResumeActions.currentResume.parent().attr('data-permissions');
   },
   getCurrentSavedStatus: function () {
      return ResumeActions.currentResume.parent().attr('data-saved');
   },
   getCurrentTrashedStatus: function () {
      return ResumeActions.currentResume.parent().attr('data-trashed');
   },
   
   share: function(resume) {
      var itemType = 'resumes';
      var tr = $('tr').filterAttr('data-id', resume);
      var possiblePermissions = $(tr).data('permissions');
      var appendTo = $(tr).find('td.resumeactions');
      if (OC.Share.droppedDown) {
         if ($(tr).data('id') != $('#dropdown').attr('data-item-source')) {
            OC.Share.hideDropDown(function () {
               $(tr).addClass('mouseOver');
               OC.Share.showDropDown(itemType, $(tr).data('id'), appendTo, true, possiblePermissions);
            });
         } else {
            OC.Share.hideDropDown();
         }
      } else {
         $(tr).addClass('mouseOver');
         OC.Share.showDropDown(itemType, $(tr).data('id'), appendTo, true, possiblePermissions);
      }
   }
};

$(document).ready(function () {
   if ($('#allowZipDownload').val() == 1) {
      var downloadScope = 'all';
   } else {
      var downloadScope = 'file';
   }

   if (typeof disableDownloadActions == 'undefined' || !disableDownloadActions) {
      ResumeActions.register(downloadScope, 'Download', OC.PERMISSION_READ, function () {
         return OC.imagePath('core', 'actions/download');
      }, function (resume) {
         window.location = OC.filePath('resumes', 'ajax', 'download.php') + '?id=' + encodeURIComponent(resume);
      });
   }

   $('#resumeList tr').on("click", "a.action", function(event) {
      event.stopPropagation();
      event.preventDefault();

      var action = $(this).data("action"); 
      ResumeActions.currentResume = $(this).closest('tr').children('td.resumename');
      if(OC.PERMISSION_READ==ResumeActions.getCurrentPermissions() && action!='Save' && action!='Unsave') return;
      var resume = ResumeActions.getCurrentId();
      var actions = ResumeActions.get(ResumeActions.getCurrentMimeType(), ResumeActions.getCurrentType(), ResumeActions.getCurrentPermissions());
      if(typeof(actions[action])) {
         actions[action](resume);
      }

      return false; 
   }); 

   // only need to load show share when showshare is present 
   if($('.showshare').length > 0)
      OC.Share.loadIcons('resumes');
});

ResumeActions.register('all', 'Share', OC.PERMISSION_READ, function () {
   return OC.imagePath('core', 'actions/share');
}, function (resume) {
   ResumeActions.share(resume); 
});

ResumeActions.register('all', 'Save', OC.PERMISSION_READ, function () {
   return OC.imagePath('core', 'actions/add');
}, function (resume) {
   ResumeList.do_save(resume);
});

ResumeActions.register('all', 'Restore', OC.PERMISSION_DELETE, function () {
   return OC.imagePath('core', 'actions/undelete.png');
}, function (resume) {
   ResumeList.do_restore(resume);
});

ResumeActions.register('all', 'Unsave', OC.PERMISSION_READ, function () {
   return OC.imagePath('core', 'actions/add');
}, function (resume) {
   ResumeList.do_unsave(resume);
});

ResumeActions.register('all', 'Trash', OC.PERMISSION_DELETE, function () {
   return OC.imagePath('core', 'actions/delete');
}, function (resume) {
   ResumeList.do_trash(resume);
});

ResumeActions.register('all', 'Permdelete', OC.PERMISSION_DELETE, function () {
   return OC.imagePath('core', 'actions/delete');
}, function (resume) {
   ResumeList.do_permdelete(resume);
});

ResumeActions.setDefault('dir', 'Open');
